<!DOCTYPE html>
<html>
<head>
    <title>问答详情</title>
    <meta charset="utf-8"/>
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <link rel="stylesheet" href="/js/bootstrap/css/bootstrap.min.css">
    <script src="/js/jquery/jquery.min.js"></script>
    <script src="/js/bootstrap/js/bootstrap.min.js"></script>
    <link rel="stylesheet" href="/css/font/css/font-awesome.min.css"/>
    <link rel="stylesheet" href="/css/reset.css"/>
    <link rel="stylesheet" href="/css/strategyCatalogs.css"/>
    <script src="/js/plugins/jrender/jrender.min.js"></script>
    <link rel="stylesheet" href="/js/plugins/dialog2/dialog.css"/>
    <script src="/js/plugins/dialog2/dialog.min.js"></script>
    <script src="/js/plugins/jquery-form/jquery.form.js"></script>
    <script src="/js/common.js"></script>

    <script>
        $(function () {
            var userStr = sessionStorage.getItem("user");
            var user;
            if (userStr) {
                user = JSON.parse(userStr);
                $("#editForm input[name='user.id']").val(user.id);
            }

            var params = getParams();
            if (params.id) {

                //每次访问页面浏览数+1
                $.get("/questions/viewCount",{"id":params.id},function (data) {
                    console.log("访问数+1");
                });

                $("#editForm input[name='questionId']").val(params.id);
                $.get("/questions/"+params.id,function (data) {
                    $("#question").renderValues(data,{
                        setHref:setHref
                    });
                    //设置采用按钮题主可见
                    if(data.user.id!=user.id){
                        $(".stateBtn").attr("style","display:none;")
                    }
                },"json");
                //渲染回答内容
                $.get("/questions/answers",{"id":params.id},function (data) {
                    $("#comment").renderValues(data,{
                        setHref:setHref,
                        setDisable:function (item, value) {
                            //设置已采用的回答其采用按钮不可见
                            if(value==1){
                                $(item).attr("style","display:none;")
                            }
                        }
                    });

                    $(".stateBtn").click(function () {
                        //找到存放了回答id的那个隐藏域
                      var answerId = $(this).next("input").val();
                        $.ajax({
                            type:'PUT',
                            url:'/questions',
                            data:{"id":answerId,"questionId":params.id},
                            success:function (data) {
                                $(document).dialog({
                                    type: 'notice',
                                    infoText: '操作成功！',
                                    autoClose: 2500,
                                    position: 'bottom'  // center: 居中; bottom: 底部
                                });
                                location.reload();
                            }
                        });
                    });

                },"json")



            }


            $(document).on('click', '#saveBtn', function() {
                $(document).dialog({
                    type : 'confirm',
                    closeBtnShow: true,
                    content: '确认您发表的言论无反动色情内容 ~',
                    onClickConfirmBtn: function(){
                        $("#editForm").ajaxSubmit(function (data) {
                            if(data.success){
                                $(document).dialog({
                                    type: 'notice',
                                    infoText: '发布成功！',
                                    autoClose: 2500,
                                    position: 'bottom'  // center: 居中; bottom: 底部
                                });
                                location.reload();
                            }
                        })
                    }
                });
            });
        })
    </script>


</head>

<body>
<div class="search-head">
    <div class="row nav-search">
        <div class="col-2">
            <a href="javascript:history.go(-1);">
                <span><i class="fa fa-chevron-left fa-2x"></i></span>
            </a>
        </div>
    </div>
</div>

<div class="container strategies" id="question">
    <h6 render-html="title"></h6>
    <small render-html="detail"></small>
    <div class="focus">
        <div class="col-2 comment-head">
            <a href="/mine/userProfiles.html?id=" render-fun="setHref" render-key="user.id">
            <img render-src="user.headImgUrl" class="rounded-circle" width="10%"/>
            </a>
        </div>
        <div class="col-3 comment-head">
            <span render-html="user.nickName"></span>
        </div>
    </div>
</div>

    <hr>
    <div id="comment">
        <div class="container row comment" id="loop" render-loop="list">
            <div>
                <div class="col-2 comment-head">
                    <a href="/mine/userProfiles.html?id=" render-fun="setHref" render-key="list.user.id">
                        <img class="rounded-circle" width="10%" render-src="list.user.headImgUrl">
                    </a>
                </div>
                <div class="col comment-right">
                    <span render-html="list.user.nickName">dafei</span>&nbsp;<span style="color:deeppink" render-html="list.stateName"></span>
                    <span class="comment-date" render-html="list.createTime">2018-07-19</span>
                    <button type="button" class="btn btn-default btn-sm stateBtn" render-fun="setDisable" render-key="list.state">采用</button>
                    <input type="hidden" name="answerId" render-value="list.id"/>
                    <div class="comment-content">
                        <p render-html="list.content">我觉得很好</p>
                        <br/>
                    </div>
                </div>
            </div>
        </div>
    </div>

<br/><br/>
    <form class="form-horizontal" action="/questions" method="post" id="editForm">
        <input type="hidden" name="user.id"/>
        <input type="hidden" name="questionId"/>
        <div class="form-group">
            <label class="col-md-4 control-label">我来回答：</label>
            <div class="col-lg-6">
                <textarea class="form-control" rows="3" name="content"></textarea>
            <button class="btn btn-success btn-sm btn-block" type="button" id="saveBtn">提交</button>
            </div>
        </div>
    </form>

</body>
</html>